﻿
using System;
using System.Web;
using System.Web.Caching;
using Xproject.Util;

namespace Core.WeiXin
{
    /**
    * 	配置账号信息
    */
    public class WxPayConfig
    {
        //=======【基本信息设置】=====================================
        /* 微信公众号信息配置
        * APPID：绑定支付的APPID（必须配置）
        * MCHID：商户号（必须配置）
        * KEY：商户支付密钥，参考开户邮件设置（必须配置）
        * APPSECRET：公众帐号secert（仅JSAPI支付的时候需要配置）
        */
        public static string APPID = "";
        public static string APPSECRET = "";

        public static string MCHID = "";
        public static string KEY = "";//API密钥

        //=======【证书路径设置】===================================== 
        /* 证书路径,注意应该填写绝对路径（仅退款、撤销订单时需要）
        */

        public static string RootPath = "";
        public const string SSLCERT_PATH = "";
        public static string SSLCERT_PASSWORD = "";
        public static string SceneId = "";//红包场景id PRODUCT_1:商品促销 PRODUCT_4:企业内部福利


        //=======【商户系统后台机器IP】===================================== 
        /* 此参数可手动配置也可在程序中自动获取
        */
        public static string IP = "";

        //=======【支付结果通知url】===================================== 
        /* 支付结果通知回调url，用于商户接收支付结果
        */
        public static string NOTIFY_URL = "";
        public static string CONFIGID = "";
        static WxPayConfig()
        {
            string key = "WxPayConfig_" + "APPID";

            APPID = System.Web.HttpRuntime.Cache[key] as string;            
            if( string.IsNullOrEmpty( APPID ) )
            {
                APPID = System.Web.Configuration.WebConfigurationManager.AppSettings["WeiXin_APPID"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, APPID, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);                 
            }

            key = "WxPayConfig_" + "APPSECRET";
            APPSECRET = System.Web.HttpRuntime.Cache[key] as string;  
            if( string.IsNullOrEmpty( APPSECRET ) )
            {
                APPSECRET = System.Web.Configuration.WebConfigurationManager.AppSettings["WeiXin_APPSECRET"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, APPSECRET, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);               
            }

            key = "WxPayConfig_" + "MCHID";
            MCHID= System.Web.HttpRuntime.Cache[key] as string;
           
            if( string.IsNullOrEmpty( MCHID ) )
            {
                MCHID = System.Web.Configuration.WebConfigurationManager.AppSettings["MCHID"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, APPSECRET, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);
              
            }

            key = "WxPayConfig_" + "KEY";
            KEY =  System.Web.HttpRuntime.Cache[key] as string;
            if ( string.IsNullOrEmpty( KEY ) )
            {
                KEY = System.Web.Configuration.WebConfigurationManager.AppSettings["MCHID_KEY"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, KEY, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);
             
            }


            key = "WxPayConfig_" + "RootPath";
            RootPath = System.Web.HttpRuntime.Cache[key] as string; 
            if( string.IsNullOrEmpty( RootPath ) )
            {
                RootPath = System.Web.Configuration.WebConfigurationManager.AppSettings["WeiXin_PAY_ROOTPATH"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, RootPath, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);
                
            }

            key = "WxPayConfig_" + "SSLCERT_PASSWORD";
            SSLCERT_PASSWORD = System.Web.HttpRuntime.Cache[key] as string;  
            if( string.IsNullOrEmpty( SSLCERT_PASSWORD ) )
            {
                SSLCERT_PASSWORD = System.Web.Configuration.WebConfigurationManager.AppSettings["WeiXin_PAY_SSLCERT_PASSWORD"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, SSLCERT_PASSWORD, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);
               
            }

            key = "WxPayConfig_" + "SceneId";
            SceneId = System.Web.HttpRuntime.Cache[key] as string; 
            if( string.IsNullOrEmpty( SceneId ) )
            {
                SceneId = System.Web.Configuration.WebConfigurationManager.AppSettings["MCHID_SceneId"];
                if( string.IsNullOrEmpty( SceneId ) ) SceneId = "null";
                System.Web.HttpRuntime.Cache.Insert(key, SceneId, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);
               
            }
            

            key = "WxPayConfig_" + "IP";
            IP = System.Web.HttpRuntime.Cache[key] as string; 
            if( string.IsNullOrEmpty( IP ) )
            {
                IP = System.Web.Configuration.WebConfigurationManager.AppSettings["MCHID_IP"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, IP, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);
                
            }
            key = "WxPayConfig_" + "CONFIGID";
            CONFIGID = System.Web.HttpRuntime.Cache[key] as string;
            if (string.IsNullOrEmpty(CONFIGID))
            {
                CONFIGID = System.Web.Configuration.WebConfigurationManager.AppSettings["CONFIGID"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, CONFIGID, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);

            }

            key = "WxPayConfig_" + "NOTIFY_URL";
            NOTIFY_URL = System.Web.HttpRuntime.Cache[key] as string; 
            if( string.IsNullOrEmpty( NOTIFY_URL ) )
            {
                string domain = System.Web.Configuration.WebConfigurationManager.AppSettings["Domain"] ?? "";
                NOTIFY_URL = domain + System.Web.Configuration.WebConfigurationManager.AppSettings["WEIXIN_NOTIFY_URL"] ?? "";
                System.Web.HttpRuntime.Cache.Insert(key, NOTIFY_URL, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration);
                
            } 
        }




        //=======【代理服务器设置】===================================
        /* 默认IP和端口号分别为0.0.0.0和0，此时不开启代理（如有需要才设置）
        */
        public const string PROXY_URL = "0.0.0.0";

        //=======【上报信息配置】===================================
        /* 测速上报等级，0.关闭上报; 1.仅错误时上报; 2.全量上报
        */
        public const int REPORT_LEVENL = 1;

        //=======【日志级别】===================================
        /* 日志等级，0.不输出日志；1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息
        */
        public static  int LOG_LEVENL = int.Parse(CommonHelper.GetAppSettings("WEIXIN_LOG_LEVENL"));
    }
}